package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.beans.Bean;
import com.demo.beans.ReturnData;
import cn.hutool.core.util.ObjectUtil;
import com.demo.service.BeanService;
import com.demo.service.Impl.BeanServiceImpl;
import com.demo.service.Impl.FilenameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("searchInfo")
public class SearchInfoController {
    @Autowired
    private BeanServiceImpl beanService;
    @Autowired
    private FilenameService filenameService;
  /*  @GetMapping("findById")
    public Bean findById(){
        String id = "001";
        Bean bean = this.beanService.getBeanById(id);
        return bean;
    }*/

    @GetMapping
    public ReturnData qbByCondition(String beginYear,
                                    String endYear,
                                    String schoolName,
                                    String branch,
                                    String major,
                                    String zdfwc,
                                    String pageIndex,
                                    String pageSize){
        ReturnData returnData = BaseAction.createReturnData();
        List<Bean> returnList = null;
        try {
            IPage<Bean> beanIPage = this.beanService.qbByCondition(beginYear, endYear, schoolName, branch, major, zdfwc, pageIndex, pageSize);
            if (zdfwc.length() > 0){
                if (beginYear.length() == 0 && endYear.length() == 0){
                    returnData.put("returnMsg","请输入开始或结束时间！");
                    return returnData;
                }
                if (branch.length() == 0){
                    returnData.put("returnMsg","请输入科类！");
                    return returnData;
                }
                /*returnData.put("returnCode","0000");
                returnData.put("returnMsg","查询成功");
                returnData.put("beans",beanList);
                return returnData;*/
            }
            if (ObjectUtil.isNotEmpty(beanIPage.getRecords())){
                returnData.put("returnCode","0000");
                returnData.put("returnMsg","查询成功");
                returnData.put("beans",beanIPage.getRecords());
                returnData.put("total",beanIPage.getTotal());
                returnData.put("size",beanIPage.getSize());
                returnData.put("current",beanIPage.getCurrent());
                returnData.put("pages",beanIPage.getPages());
                return returnData;
            }else {
                returnData.put("returnCode","9999");
                returnData.put("returnMsg","未查询到数据");
            }
        } catch (Exception e) {
                returnData.put("returnCode","9999");
                returnData.put("returnMsg","查询异常！请检查查询条件：" + e);
            e.printStackTrace();
        }
        return returnData;
    }
    @PostMapping("deleteBean")
    public ReturnData deleteBean(@RequestParam("action") String action, @RequestParam("fileNameId") String  fileNameId){
        ReturnData returnData = BaseAction.createReturnData();
        if ("sure".equals(action)){
            boolean b = this.beanService.sureSaveBean();
            if (b){
                returnData.put("returnCode","0000");
                returnData.put("action","sure");
                returnData.put("returnMsg","确认保存成功。");
            }else {
                returnData.put("returnMsg","确认保存失败！！");
            }
            return returnData;
        }
        if ("rollback".equals(action)){
            boolean r = this.beanService.rollbackBean();
            boolean b = this.filenameService.rollbackFilename(fileNameId);
            if (r && b){
                returnData.put("returnCode","0000");
                returnData.put("action","rollback");
                returnData.put("returnMsg","回滚成功。");
            }else {
                returnData.put("returnMsg","回滚失败！！");
            }
            return returnData;
        }
        if ("deleteDatabase".equals(action)){
            boolean r = this.beanService.deleteDatabase();
            boolean b = this.filenameService.deleteDatabase();
            if (r && b){
                returnData.put("returnCode","0000");
                returnData.put("action","deleteDatabase");
                returnData.put("returnMsg","删除数据库成功。");
            }else {
                returnData.put("returnMsg","删除数据库失败！！");
            }
            return returnData;
        }
        return returnData;
    }
}
